package com.ymatou.query.seller.trading.api.testcase;

import java.math.BigDecimal;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.query.seller.trading.api.parameter.AmountInfo;
import com.ymatou.query.seller.trading.api.parameter.GetSellerSalesDetailBean;
import com.ymatou.query.seller.trading.api.parameter.GetSellerSalesDetailResponseV2;
import com.ymatou.query.seller.trading.api.parameter.GetSellerSalesDetailResult;
import com.ymatou.query.seller.trading.api.parameter.Sp_GetSellerSalesDetail_v2;
import com.ymatou.query.seller.trading.api.service.GetSellerSalesDetailV2Call;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.EnvSetup;

/**
 * 获取卖家销售额明细
 */
@SuppressWarnings("rawtypes")
@TestSuite
public class Ts_GetSellerSalesDetailV2 {

	private static GetSellerSalesDetailBean getSellerSalesDetailBean;
	private static GetSellerSalesDetailV2Call getSellerSalesDetailV2Call;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取卖家销售额明细");
	}

	@Before
	public void caseUp() {
		getSellerSalesDetailBean = new GetSellerSalesDetailBean();
		getSellerSalesDetailV2Call = new GetSellerSalesDetailV2Call();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getSellerSalesDetailV2Call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	private void verify() {
		GetSellerSalesDetailResponseV2 expectedResponse = new GetSellerSalesDetailResponseV2();
		ConstructExpectedResponse(expectedResponse);
		GetSellerSalesDetailResponseV2 actualResponse = getSellerSalesDetailV2Call
				.getSellerSalesDetailResponseV2();
		Logger.comment("***********************开始验证Sales.Today节点***********************");
		GetSellerSalesDetailResponseV2.StatusAmountSummaryV2 expSalesToday = expectedResponse.getSales().getToday();
		GetSellerSalesDetailResponseV2.StatusAmountSummaryV2 actSalesToday = actualResponse.getSales().getToday();
		Logger.comment("开始验证Unpaid节点");
		AmountInfo expSalesTodayUnPaid =  expSalesToday.getUnpaid();
		AmountInfo actSalesTodayUnPaid =  actSalesToday.getUnpaid();
		verifyAmountInfo(expSalesTodayUnPaid, actSalesTodayUnPaid);
		Logger.comment("完成验证Unpaid节点");
		Logger.comment("开始验证PartialPaid节点");
		AmountInfo expSalesTodayPartialPaid =  expSalesToday.getPartialPaid();
		AmountInfo actSalesTodayPartialPaid =  actSalesToday.getPartialPaid();
		verifyAmountInfo(expSalesTodayPartialPaid, actSalesTodayPartialPaid);
		Logger.comment("完成验证PartialPaid节点");
		Logger.comment("开始验证FullPaid节点");
		AmountInfo expSalesTodayFullPaid =  expSalesToday.getFullPaid();
		AmountInfo actSalesTodayFullPaid =  actSalesToday.getFullPaid();
		verifyAmountInfo(expSalesTodayFullPaid, actSalesTodayFullPaid);
		Logger.comment("完成验证FullPaid节点");
		Logger.comment("开始验证FullPaid节点");
		AmountInfo expSalesTodayCancelled =  expSalesToday.getCancelled();
		AmountInfo actSalesTodayCancelled =  actSalesToday.getCancelled();
		verifyAmountInfo(expSalesTodayCancelled, actSalesTodayCancelled);
		Logger.comment("完成验证FullPaid节点");
		Logger.comment("***********************完成验证Sales.Today节点***********************");
	
		Logger.comment("***********************开始验证Sales.Yesterday节点***********************");
		GetSellerSalesDetailResponseV2.StatusAmountSummaryV2 expSalesYesterday = expectedResponse.getSales().getYesterday();
		GetSellerSalesDetailResponseV2.StatusAmountSummaryV2 actSalesYesterday = actualResponse.getSales().getYesterday();
		Logger.comment("开始验证Unpaid节点");
		AmountInfo expSalesYesterdayUnPaid =  expSalesYesterday.getUnpaid();
		AmountInfo actSalesYesterdayUnPaid =  actSalesYesterday.getUnpaid();
		verifyAmountInfo(expSalesYesterdayUnPaid, actSalesYesterdayUnPaid);
		Logger.comment("完成验证Unpaid节点");
		Logger.comment("开始验证PartialPaid节点");
		AmountInfo expSalesYesterdayPartialPaid =  expSalesYesterday.getPartialPaid();
		AmountInfo actSalesYesterdayPartialPaid =  actSalesYesterday.getPartialPaid();
		verifyAmountInfo(expSalesYesterdayPartialPaid, actSalesYesterdayPartialPaid);
		Logger.comment("完成验证PartialPaid节点");
		Logger.comment("开始验证FullPaid节点");
		AmountInfo expSalesYesterdayFullPaid =  expSalesYesterday.getFullPaid();
		AmountInfo actSalesYesterdayFullPaid =  actSalesYesterday.getFullPaid();
		verifyAmountInfo(expSalesYesterdayFullPaid, actSalesYesterdayFullPaid);
		Logger.comment("完成验证FullPaid节点");
		Logger.comment("开始验证FullPaid节点");
		AmountInfo expSalesYesterdayCancelled =  expSalesYesterday.getCancelled();
		AmountInfo actSalesYesterdayCancelled =  actSalesYesterday.getCancelled();
		verifyAmountInfo(expSalesYesterdayCancelled, actSalesYesterdayCancelled);
		Logger.comment("完成验证FullPaid节点");
		Logger.comment("***********************完成验证Sales.Yesterday节点***********************");
		
		Logger.comment("***********************开始验证Sales.Week节点***********************");
		GetSellerSalesDetailResponseV2.StatusAmountSummaryV2 expSalesWeek = expectedResponse.getSales().getWeek();
		GetSellerSalesDetailResponseV2.StatusAmountSummaryV2 actSalesWeek = actualResponse.getSales().getWeek();
		Logger.comment("开始验证Unpaid节点");
		AmountInfo expSalesWeekUnPaid =  expSalesWeek.getUnpaid();
		AmountInfo actSalesWeekUnPaid =  actSalesWeek.getUnpaid();
		verifyAmountInfo(expSalesWeekUnPaid, actSalesWeekUnPaid);
		Logger.comment("完成验证Unpaid节点");
		Logger.comment("开始验证PartialPaid节点");
		AmountInfo expSalesWeekPartialPaid =  expSalesWeek.getPartialPaid();
		AmountInfo actSalesWeekPartialPaid =  actSalesWeek.getPartialPaid();
		verifyAmountInfo(expSalesWeekPartialPaid, actSalesWeekPartialPaid);
		Logger.comment("完成验证PartialPaid节点");
		Logger.comment("开始验证FullPaid节点");
		AmountInfo expSalesWeekFullPaid =  expSalesWeek.getFullPaid();
		AmountInfo actSalesWeekFullPaid =  actSalesWeek.getFullPaid();
		verifyAmountInfo(expSalesWeekFullPaid, actSalesWeekFullPaid);
		Logger.comment("完成验证FullPaid节点");
		Logger.comment("开始验证FullPaid节点");
		AmountInfo expSalesWeekCancelled =  expSalesWeek.getCancelled();
		AmountInfo actSalesWeekCancelled =  actSalesWeek.getCancelled();
		verifyAmountInfo(expSalesWeekCancelled, actSalesWeekCancelled);
		Logger.comment("完成验证FullPaid节点");
		Logger.comment("***********************完成验证Sales.Week节点***********************");
		
		Logger.comment("***********************开始验证Sales.Month节点***********************");
		GetSellerSalesDetailResponseV2.StatusAmountSummaryV2 expSalesMonth = expectedResponse.getSales().getMonth();
		GetSellerSalesDetailResponseV2.StatusAmountSummaryV2 actSalesMonth = actualResponse.getSales().getMonth();
		Logger.comment("开始验证Unpaid节点");
		AmountInfo expSalesMonthUnPaid =  expSalesMonth.getUnpaid();
		AmountInfo actSalesMonthUnPaid =  actSalesMonth.getUnpaid();
		verifyAmountInfo(expSalesMonthUnPaid, actSalesMonthUnPaid);
		Logger.comment("完成验证Unpaid节点");
		Logger.comment("开始验证PartialPaid节点");
		AmountInfo expSalesMonthPartialPaid =  expSalesMonth.getPartialPaid();
		AmountInfo actSalesMonthPartialPaid =  actSalesMonth.getPartialPaid();
		verifyAmountInfo(expSalesMonthPartialPaid, actSalesMonthPartialPaid);
		Logger.comment("完成验证PartialPaid节点");
		Logger.comment("开始验证FullPaid节点");
		AmountInfo expSalesMonthFullPaid =  expSalesMonth.getFullPaid();
		AmountInfo actSalesMonthFullPaid =  actSalesMonth.getFullPaid();
		verifyAmountInfo(expSalesMonthFullPaid, actSalesMonthFullPaid);
		Logger.comment("完成验证FullPaid节点");
		Logger.comment("开始验证FullPaid节点");
		AmountInfo expSalesMonthCancelled =  expSalesMonth.getCancelled();
		AmountInfo actSalesMonthCancelled =  actSalesMonth.getCancelled();
		verifyAmountInfo(expSalesMonthCancelled, actSalesMonthCancelled);
		Logger.comment("完成验证FullPaid节点");
		Logger.comment("***********************完成验证Sales.Month节点***********************");
	}
	
	private void verifyAmountInfo(AmountInfo expect,AmountInfo actual){
		DataUtil.verifyDecimalEquals(expect.getShangou(), actual.getShangou(), "验证Shangou");
		DataUtil.verifyDecimalEquals(expect.getSpot(), actual.getSpot(), "验证Spot");
		DataUtil.verifyDecimalEquals(expect.getSellerCoupon(), actual.getSellerCoupon(), "验证SellerCoupon");
	}

	private void ConstructExpectedResponse(
			GetSellerSalesDetailResponseV2 response) {
		Sp_GetSellerSalesDetail_v2 sp = new Sp_GetSellerSalesDetail_v2();
		sp.setSellerId(DataUtil.Integerr
				.GetValueOrDefault(getSellerSalesDetailBean.getSellerId()));
		sp.execute();
		Map yesterday = sp.getYesterday();
		Map today = sp.getToday();
		Map week = sp.getWeek();
		Map month = sp.getMonth();
		GetSellerSalesDetailResult result = mapToBean(yesterday, today, week,
				month);
		GetSellerSalesDetailResponseV2.PeriodStatusAmountSummaryV2 o = new GetSellerSalesDetailResponseV2.PeriodStatusAmountSummaryV2();
		o.setYesterday(GetDetail(result.getYesterday()));
		o.setToday(GetDetail(result.getToday()));
		o.setWeek(GetDetail(result.getWeek()));
		o.setMonth(GetDetail(result.getMonth()));
		response.setSales(o);
	}

	private static GetSellerSalesDetailResponseV2.StatusAmountSummaryV2 GetDetail(
			GetSellerSalesDetailResult.SalesDetail detail) {
		GetSellerSalesDetailResponseV2.StatusAmountSummaryV2 o = new GetSellerSalesDetailResponseV2.StatusAmountSummaryV2();
		o.setUnpaid(GetAmount(detail.getUnpaid_Spot(),
				detail.getUnpaid_Shangou(), detail.getUnpaid_SellerCoupon()));
		o.setPartialPaid(GetAmount(detail.getPartialPaid_Spot(),
				detail.getPartialPaid_Shangou(),
				detail.getPartialPaid_SellerCoupon()));
		o.setFullPaid(GetAmount(detail.getFullPaid_Spot(),
				detail.getFullPaid_Shangou(), detail.getFullPaid_SellerCoupon()));
		o.setCancelled(GetAmount(detail.getCancelled_Spot(),
				detail.getCancelled_Shangou(),
				detail.getCancelled_SellerCoupon()));
		o.setTotal(GetTotal(o.getUnpaid()).add(GetTotal(o.getPartialPaid()))
				.add(GetTotal(o.getFullPaid()).add(GetTotal(o.getCancelled()))));
		return o;
	}

	private static BigDecimal GetTotal(AmountInfo o) {
		return DataUtil.BigDecimall.GetValueOrDefault(o.getSpot()).add(
				DataUtil.BigDecimall.GetValueOrDefault(o.getShangou()));
	}

	private static AmountInfo GetAmount(BigDecimal spot, BigDecimal shangou,
			BigDecimal sellerCoupon) {
		AmountInfo o = new AmountInfo();
		o.setSpot(spot);
		o.setShangou(shangou);
		o.setSellerCoupon(sellerCoupon);
		return o;
	}

	private GetSellerSalesDetailResult mapToBean(Map yesterday, Map today,
			Map week, Map month) {
		GetSellerSalesDetailResult o = new GetSellerSalesDetailResult();
		GetSellerSalesDetailResult.SalesDetail zuotian = new GetSellerSalesDetailResult.SalesDetail();
		zuotian.setUnpaid_Spot(DataUtil.BigDecimall.GetValueOrDefault(yesterday
				.get("Unpaid_Spot")));
		zuotian.setUnpaid_Shangou(DataUtil.BigDecimall
				.GetValueOrDefault(yesterday.get("Unpaid_Shangou")));
		zuotian.setUnpaid_SellerCoupon(DataUtil.BigDecimall
				.GetValueOrDefault(yesterday.get("Unpaid_SellerCoupon")));
		zuotian.setPartialPaid_Spot(DataUtil.BigDecimall
				.GetValueOrDefault(yesterday.get("PartialPaid_Spot")));
		zuotian.setPartialPaid_Shangou(DataUtil.BigDecimall
				.GetValueOrDefault(yesterday.get("PartialPaid_Shangou")));
		zuotian.setPartialPaid_SellerCoupon(DataUtil.BigDecimall
				.GetValueOrDefault(yesterday.get("PartialPaid_SellerCoupon")));
		zuotian.setFullPaid_Spot(DataUtil.BigDecimall
				.GetValueOrDefault(yesterday.get("FullPaid_Spot")));
		zuotian.setFullPaid_Shangou(DataUtil.BigDecimall
				.GetValueOrDefault(yesterday.get("FullPaid_Shangou")));
		zuotian.setFullPaid_SellerCoupon(DataUtil.BigDecimall
				.GetValueOrDefault(yesterday.get("FullPaid_SellerCoupon")));
		zuotian.setCancelled_Spot(DataUtil.BigDecimall
				.GetValueOrDefault(yesterday.get("Cancelled_Spot")));
		zuotian.setCancelled_Shangou(DataUtil.BigDecimall
				.GetValueOrDefault(yesterday.get("Cancelled_Shangou")));
		zuotian.setCancelled_SellerCoupon(DataUtil.BigDecimall
				.GetValueOrDefault(yesterday.get("Cancelled_SellerCoupon")));
		o.setYesterday(zuotian);

		GetSellerSalesDetailResult.SalesDetail jintian = new GetSellerSalesDetailResult.SalesDetail();
		jintian.setUnpaid_Spot(DataUtil.BigDecimall.GetValueOrDefault(today
				.get("Unpaid_Spot")));
		jintian.setUnpaid_Shangou(DataUtil.BigDecimall.GetValueOrDefault(today
				.get("Unpaid_Shangou")));
		jintian.setUnpaid_SellerCoupon(DataUtil.BigDecimall
				.GetValueOrDefault(today.get("Unpaid_SellerCoupon")));
		jintian.setPartialPaid_Spot(DataUtil.BigDecimall
				.GetValueOrDefault(today.get("PartialPaid_Spot")));
		jintian.setPartialPaid_Shangou(DataUtil.BigDecimall
				.GetValueOrDefault(today.get("PartialPaid_Shangou")));
		jintian.setPartialPaid_SellerCoupon(DataUtil.BigDecimall
				.GetValueOrDefault(today.get("PartialPaid_SellerCoupon")));
		jintian.setFullPaid_Spot(DataUtil.BigDecimall.GetValueOrDefault(today
				.get("FullPaid_Spot")));
		jintian.setFullPaid_Shangou(DataUtil.BigDecimall
				.GetValueOrDefault(today.get("FullPaid_Shangou")));
		jintian.setFullPaid_SellerCoupon(DataUtil.BigDecimall
				.GetValueOrDefault(today.get("FullPaid_SellerCoupon")));
		jintian.setCancelled_Spot(DataUtil.BigDecimall.GetValueOrDefault(today
				.get("Cancelled_Spot")));
		jintian.setCancelled_Shangou(DataUtil.BigDecimall
				.GetValueOrDefault(today.get("Cancelled_Shangou")));
		jintian.setCancelled_SellerCoupon(DataUtil.BigDecimall
				.GetValueOrDefault(today.get("Cancelled_SellerCoupon")));
		o.setToday(jintian);

		GetSellerSalesDetailResult.SalesDetail benzhou = new GetSellerSalesDetailResult.SalesDetail();
		benzhou.setUnpaid_Spot(DataUtil.BigDecimall.GetValueOrDefault(week
				.get("Unpaid_Spot")));
		benzhou.setUnpaid_Shangou(DataUtil.BigDecimall.GetValueOrDefault(week
				.get("Unpaid_Shangou")));
		benzhou.setUnpaid_SellerCoupon(DataUtil.BigDecimall
				.GetValueOrDefault(week.get("Unpaid_SellerCoupon")));
		benzhou.setPartialPaid_Spot(DataUtil.BigDecimall.GetValueOrDefault(week
				.get("PartialPaid_Spot")));
		benzhou.setPartialPaid_Shangou(DataUtil.BigDecimall
				.GetValueOrDefault(week.get("PartialPaid_Shangou")));
		benzhou.setPartialPaid_SellerCoupon(DataUtil.BigDecimall
				.GetValueOrDefault(week.get("PartialPaid_SellerCoupon")));
		benzhou.setFullPaid_Spot(DataUtil.BigDecimall.GetValueOrDefault(week
				.get("FullPaid_Spot")));
		benzhou.setFullPaid_Shangou(DataUtil.BigDecimall.GetValueOrDefault(week
				.get("FullPaid_Shangou")));
		benzhou.setFullPaid_SellerCoupon(DataUtil.BigDecimall
				.GetValueOrDefault(week.get("FullPaid_SellerCoupon")));
		benzhou.setCancelled_Spot(DataUtil.BigDecimall.GetValueOrDefault(week
				.get("Cancelled_Spot")));
		benzhou.setCancelled_Shangou(DataUtil.BigDecimall
				.GetValueOrDefault(week.get("Cancelled_Shangou")));
		benzhou.setCancelled_SellerCoupon(DataUtil.BigDecimall
				.GetValueOrDefault(week.get("Cancelled_SellerCoupon")));
		o.setWeek(benzhou);

		GetSellerSalesDetailResult.SalesDetail benyue = new GetSellerSalesDetailResult.SalesDetail();
		benyue.setUnpaid_Spot(DataUtil.BigDecimall.GetValueOrDefault(month
				.get("Unpaid_Spot")));
		benyue.setUnpaid_Shangou(DataUtil.BigDecimall.GetValueOrDefault(month
				.get("Unpaid_Shangou")));
		benyue.setUnpaid_SellerCoupon(DataUtil.BigDecimall
				.GetValueOrDefault(month.get("Unpaid_SellerCoupon")));
		benyue.setPartialPaid_Spot(DataUtil.BigDecimall.GetValueOrDefault(month
				.get("PartialPaid_Spot")));
		benyue.setPartialPaid_Shangou(DataUtil.BigDecimall
				.GetValueOrDefault(month.get("PartialPaid_Shangou")));
		benyue.setPartialPaid_SellerCoupon(DataUtil.BigDecimall
				.GetValueOrDefault(month.get("PartialPaid_SellerCoupon")));
		benyue.setFullPaid_Spot(DataUtil.BigDecimall.GetValueOrDefault(month
				.get("FullPaid_Spot")));
		benyue.setFullPaid_Shangou(DataUtil.BigDecimall.GetValueOrDefault(month
				.get("FullPaid_Shangou")));
		benyue.setFullPaid_SellerCoupon(DataUtil.BigDecimall
				.GetValueOrDefault(month.get("FullPaid_SellerCoupon")));
		benyue.setCancelled_Spot(DataUtil.BigDecimall.GetValueOrDefault(month
				.get("Cancelled_Spot")));
		benyue.setCancelled_Shangou(DataUtil.BigDecimall
				.GetValueOrDefault(month.get("Cancelled_Shangou")));
		benyue.setCancelled_SellerCoupon(DataUtil.BigDecimall
				.GetValueOrDefault(month.get("Cancelled_SellerCoupon")));
		o.setMonth(benyue);

		return o;
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetSellerSalesInfoV2_001() {
		Logger.start(true, "获取卖家销售额明细成功");
		try {
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			getSellerSalesDetailBean.setSellerId(sellerId);
			getSellerSalesDetailV2Call.setData(getSellerSalesDetailBean);
			getSellerSalesDetailV2Call.callService();
			Logger.comment("getSellerSalesDetailV2Call send:"
					+ getSellerSalesDetailV2Call.getEntityString());
			Logger.comment("getSellerSalesDetailV2Call response:"
					+ getSellerSalesDetailV2Call.getReturnData());
			Logger.verifyEquals("200", getSellerSalesDetailV2Call.getCode(),
					"验证code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}